<?php

namespace App\Http\Controllers;

use App\Models\Containers;
use App\Models\Packages;
use App\Models\Staffs;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Mockery\Exception;

class ContainerController extends Controller
{
    //
    public function getByStaff(Request $request){
        $containers = Containers::where("staffs_id" , $request->object_id)->with(['from_campus' , 'to_campus'])->get();
        $json = [];
        foreach ($containers as $container) {
            $json[] = [
                'id' => $container->id,
                'from_campus' => $container->from_campus->name,
                'to_campus' => $container->to_campus->name,
                'packages' => $container->quantity
            ];
        }
        return $this->returnJson($json);
    }

    public function getByCampus(Request $request){
        $staff = Staffs::where("id" , $request->object_id)->first();
    dd($staff);
        $containers = Containers::where("from_campus_id" , $staff->campus_id)->with(['from_campus' , 'to_campus'])->get();
        $json = [];
        foreach ($containers as $container) {
            $json[] = [
                'id' => $container->id,
                'from_campus' => $container->from_campus->name,
                'to_campus' => $container->to_campus->name,
                'packages' => $container->quantity
            ];
        }
        return $this->returnJson($json);
    }

    public function loadContainer($container_id , Request $request){
        $staff = Staffs::where("id" , $request->object_id)->first();
        $container = Containers::where("id" , $container_id)->where("from_campus_id" , $staff->campus_id)->get();
        if ($container == null) {
            return $this->returnNotFoundError();
        }
        if (15 - $staff->package_number < $container->quantity) {
            return $this->returnAlreadyError("over the trucker's remaining capacity" , [
                'remaining_capacity' => 15 - $staff->package_number
            ]);
        }
        return $this->returnJson();
    }

    // TODO : 缺少退货打包回去的功能
    public function unloadContainer($container_id){
        $packages = Packages::where("container_id" , $container_id)->get();
        $return_packages = [];
        foreach ($packages as $package) {
            if ($package->is_returing == '1'){
                $container = Containers::where("from_campus_id" , $package->from_campus_id)
                    ->where("to_campus_id" , $package->to_campus_id)
                    ->whereNull("staff_id")
                    ->where("quantity"  , "<" , 5)
                    ->first();
                if ($container == null){
                    try {
                        DB::beginTransaction();
                        $container = Containers::create([
                            'from_campus_id' => $package->from_campus_id,
                            'to_campus_id' => $package->to_campus_id,
                            'quantity' => 1,
                        ]);
                        $package->containers_id = $container->id;
                        $package->save();
                        DB::commit();
                    }catch (Exception $e){
                        DB::rollback();
                    }
                }else{
                    try {
                        DB::beginTransaction();
                        $container->quantity = $container->quantity + 1;
                        $container->save();
                        $package->containers_id = $container->id;
                        $package->save();
                        DB::commit();
                    }catch (Exception $e){
                        DB::rollBack();
                    }
                }
                $return_packages[] = [
                    'package_id' => $package->id,
                    'container_id' => $container->id,
                ];
            }
        }
        return $this->returnJson($return_packages);
    }
}
